﻿using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

using System.Collections;
using System;

[Serializable]
public class BTViewBase
{

    #region Public Variables
    public string viewTitle;
    public Rect viewRect;
    #endregion

    #region Protected Variables
    protected GUISkin viewSkin;
    protected BTNodeGraph curGraph;

    protected Rect maxViewRect;
    #endregion

    #region Constructors
    public BTViewBase(string title)
    {
        viewTitle = title;
        
    }
    #endregion

    #region Main Methods
    void OnEnable()
    {
        GetEditorSkin();
    }

    public virtual void UpdateView(Rect editorRect, Rect precentageRect, Event e, BTNodeGraph curGraph)
    {
        if(viewSkin == null)
        {
            GetEditorSkin();
            return;
        }

        viewRect = new Rect(editorRect.x * precentageRect.x,
                            editorRect.y * precentageRect.y,
                            editorRect.width * precentageRect.width,
                           editorRect.height * precentageRect.height);
        maxViewRect = viewRect;

        this.curGraph = curGraph;
        if(this.curGraph != null)
        {
            this.viewTitle = curGraph.GraphName;

            if(this.curGraph.nodes.Count > 0)
            {
                float maxX = 0f;
                float maxY = 0f;
                float nodeX;
                float nodeY;
                foreach (BTNodeBase node in this.curGraph.nodes)
                {
                    nodeX = node.nodeRect.x + node.nodeRect.width;
                    if (nodeX > maxX)
                    {
                        maxX = nodeX;
                    }
                    nodeY = node.nodeRect.y + node.nodeRect.height;
                    if (nodeY > maxY)
                    {
                        maxY = nodeY;
                    }
                }

                if(maxX > viewRect.width)
                {
                    maxViewRect.width = maxX;
                }

                if (maxY > viewRect.height)
                {
                    maxViewRect.height = maxY;
                }
            }
            
        }
        else
        {
            this.viewTitle = "No Graph";
        }

        


    }

    public virtual void ProcessEvents(Event e) { }
    #endregion

    #region Utility Methods
    protected void GetEditorSkin()
    {
        //viewSkin = (GUISkin)Resources.Load(BTreeEditorData.pathGUISkins);
        viewSkin = AssetDatabase.LoadAssetAtPath<GUISkin>(BTreeEditorData.pathGUISkins);

    }
    #endregion
}
